--[[
   create at 2018-11-30 16:39:17
   author:刘易周
   @brief:
--]]
g_PreRes = nil
g_scale = 20
g_distance = nil
g_initialStandardSwitch = true
g_disRatio = 0
g_score_path = {
  "f_score/f_score_0",
  "f_score/f_score_10",
  "f_score/f_score_20",
  "f_score/f_score_30",
  "f_score/f_score_40",
  "f_score/f_score_50",
  "f_score/f_score_60",
  "f_score/f_score_70",
  "f_score/f_score_80",
  "f_score/f_score_90",
  "f_score/f_score_100"
}
g_socre_node={}
g_sampler={}
--call once when scene loaded
function initialize(scene)
  g_PreRes = scene:getResolution()
  local kuruEngineInstance = KuruEngine.getInstance()
  kuruFaceDetector = KuruFaceDetectorExtension.cast(kuruEngineInstance:getExtension("KuruFaceDetector"))
  kuruTouch = KuruTouchExtension.cast(kuruEngineInstance:getExtension("KuruTouch"))
  kuruTouch:getTouchDownEvent():addEventHandler(onTouchDown)

  for i = 1, #g_score_path,1  do

    g_sampler[i] = getAnimationSampler(g_score_path[i])
    
    g_socre_node[i] = KaleStickerNode.createFromSampler(g_sampler[i], BlendMode.None, 0, 0)
    -- print("KURU微笑的分 : " .. "得分")
    g_socre_node[i]:setLocationType(KaleStickerNodeLocationType.EYES_CENTER)
    g_socre_node[i]:setAnchorType(KaleStickerNodeAnchorType.CENTER)
    g_socre_node[i]:setTranslation(0, -1.0, 0.0)   
    g_sampler[i]:release()
    g_socre_node[i] = addNodeAndRelease(scene, g_socre_node[i])
    g_socre_node[i]:setEnabled(false)
    
  end
end

--call every frame,elapsed time is the time elapsed this frame
function frameReady(scene, elapsedTime)
  -- print("KURU")
  if kuruFaceDetector then
    if kuruFaceDetector:getFaceCount() > 0 then
      local faceFeature = KaleFaceFeature.cast(kuruFaceDetector:getFace(0))
      local faceData = faceFeature:getFaceData()
      local rightLipPos = faceData:getUlseeVertexShape(48)
      local leftLipPos = faceData:getUlseeVertexShape(54)

      local righteyePos = faceData:getUlseeVertexShape(39)
      local lefteyePos = faceData:getUlseeVertexShape(42)

      DisEye = righteyePos.x - lefteyePos.x --眼角的距离
      DisLips = rightLipPos.x - leftLipPos.x --嘴距

      deltaDis = DisLips - DisEye --嘴距-眼距

      setScore(deltaDis, DisLips)

      -- print("KURU嘴巴X : " .. rightLipPos.x.."______"..leftLipPos.x)
      -- print("KURU嘴巴间距 : " .. rightLipPos.x-leftLipPos.x)

      -- -- print("KURUrightY : " .. rightLipPos.y)
      -- print("KURU眼镜X : " .. righteyePos.x.."______"..lefteyePos.x)
      -- print("KURU眼镜X间距: " .. righteyePos.x-lefteyePos.x)
      -- -- print("KURUleftY : " .. leftLipPos.y)
      -- print("KURU左眼与左嘴间距 : " .. leftLipPos.x-lefteyePos.x)
      -- getSensetimeVertexShape 
      -- print("KURU右眼与右嘴间距 : " .. faceData:getUlseeVertexShape (106).x)
     
      for i = 1,  #g_score_path,1 do
        if score / 10+1 == i then
          print("KURU微笑的分 : " .. "得分" .. score)
          g_socre_node[i]:setEnabled(true)
        else
          g_socre_node[i]:setEnabled(false)
        end
      end
    end
  end
end

-- 评分 人的嘴巴张开程度为嘴巴的1/3大小
function setScore(deltaDis, DisLips)
  score = math.floor(deltaDis / DisLips * 33.4) * 10
  if score <= 0 then
    score = 0
  end
  if score >= 100 then
    score = 100
  end
end

--call when reset scene,e.g switch to rear camera
function reset(scene)
end

--call when scene close
function finalize(scene)
  kuruTouch:getTouchDownEvent():removeEventHandler(onTouchDown)
end

--call when touch screen
function onTouchDown(event)
  print("touchdown")
end

function addNodeAndRelease(scene, node)
  scene:addNode(node)
  node:release()
  return node
end

function addChildAndRelease(parent, child)
  parent:addChild(child)
  child:release()
  return child
end

function getAnimationSampler(filePath)
  local sampler = KuruAnimationSampler.createFromPath(BASE_DIRECTORY .. filePath, false, false)

  sampler:setRepeatCount(0)
  sampler:setFPS(20)
  sampler:play()
  sampler:setWrapMode(TextureWrap.CLAMP, TextureWrap.CLAMP)

  return sampler
end
